<?php 
/*
 * Save the Person's (student or faculty details into database & redirect to DisplayPage with 
 * a new variable called shwetha_result.
 * */
session_start();

$isStudent = false;  // true - student; false - faculty

$ssn = $_POST["ssn_name"];
$firstName = $_POST["FName"];
$lastName = $_POST["LName"];
$email = $_POST["Email"];
$apt = $_POST["AptNo"];
$st = $_POST["StName"];
$city = $_POST["cityNo"];
$state = $_POST["state"];
$zip = $_POST["zip"];

if (isset($_POST["gpa"])) {  $gpa = $_POST["gpa"]; }
if (isset($_POST["major"])) {  $major = $_POST["major"]; $isStudent = true; }
if (isset($_POST["term"])) {  $term = $_POST["term"]; }
if (isset($_POST["fees"])) {  $fees = $_POST["fees"]; }

if (isset($_POST["designation"])) {  $designation = $_POST["designation"]; }
if (isset($_POST["salary"])) {  $salary = $_POST["salary"]; }



// DB connect
require "config.php";
$con = mysql_connect("localhost",$db_user ,$db_pass);
if (!$con){
	$_SESSION["shwetha_result"] = "Database cannot connect. Changes not saved.";
	header('Location: DisplayPage.php');
}
mysql_select_db($db_name, $con);


// Update Person
$format = "update person set firstName = '%s',
							 lastName  = '%s', 
						     email     = '%s', 
						     apartmentNumber = '%s',
							 streetNumber = '%s',
							 city ='%s',
							 state = '%s',
							 zipcode = '%s'
							 where (SSN = $ssn );
							 ";

$query = sprintf($format, $firstName, $lastName, $email, $apt, $st, $city, $state, $zip);
$q_result = mysql_query($query);
if( !$q_result )
{
	$_SESSION["shwetha_result"] = "Changes cannot be saved. DB person table update error";
	header('Location: DisplayPage.php');
}

// Update Student
if ( $isStudent) {
	$format = "update student set ";
	if (isset($gpa)) {
		$format .= " GPA = '".$gpa."',";
	}
	if (isset($major)) {
		$format .= " major = '".$major."',";
	}
	if (isset($term)) {
		$format .= " term = '".$term."'";
	}
	if (isset($fees)) {
		$format .= ", fees = '".$fees."'";
	}
	$format .= " where (SSN = '".$ssn."')"; 
	$q_result = mysql_query($format);
	if( !$q_result )
	{
		$_SESSION["shwetha_result"] = "Changes cannot be saved. DB person table update error";
		header('Location: DisplayPage.php');
	}
} else {
// Update faculty 
	$format = "update faculty set ";
	if (isset($salary)) {
		$format .= " salary = '".$salary."',";
	}
	if (isset($designation)) {
		$format .= " designation = '".$designation."'";
	}
	$format .= " where (SSN = '".$ssn."')";
	$q_result = mysql_query($format);
	if( !$q_result )
	{
		$_SESSION["shwetha_result"] = "Changes cannot be saved. DB person table update error";
		header('Location: DisplayPage.php');
	}
}


//$_SESSION["shwetha_result"] = "Successfully Saved !!";
$_SESSION["shwetha_result"] = $format;
header('Location: DisplayPage.php');

?>